﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataTier;
using System.Data.SqlClient;
using System.Data.Common;

namespace DbLayer
{
    public class DbStation : DbObject, IDbStation
    {

        private static DbStation _instance = null;
        private IDbLocation _dbLocation = DbLocation.Instance;
        private IDbCity _dbCity = DbCity.Instance;

        public static DbStation Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new DbStation();
                }
                return _instance;
            }
        }

        public void AddStation(Station station)
        {
            string statement = BuildInsertStatement("Station", GetNextId("Station", "Id").ToString(), station.Address, station.ZipCode.ToString(), station.Location.Id.ToString(), station.AvailableBatteries.ToString());
            ExecuteStatement(statement);
        }

        public IList<Station> GetAllStations()
        {

            IList<Station> stations = new List<Station>();
            string statement = BuildSelectStatement("Station", "");
            SqlDataReader reader = ExecuteQuery(statement);

            while (reader.Read())
            {
                stations.Add(BuildStation(reader));
            }

            reader.Close();

            return stations;

        }

        public Station GetStation(int stationId)
        {

            Station station = null;
            string statement = BuildSelectStatement("Station", "id = " + stationId.ToString());
            SqlDataReader reader = ExecuteQuery(statement);

            if (reader.Read())
            {
                station = BuildStation(reader);
            }

            return station;

        }

        public Station GetStationFromLocation(int locationId)
        {

            Station station = null;
            string statement = BuildSelectStatement("Station", "locationId = " + locationId.ToString());
            SqlDataReader reader = ExecuteQuery(statement);

            if (reader.Read())
            {
                station = BuildStation(reader);
            }

            return station;

        }

        public void DeleteStation(int stationId)
        {
            string wClause = "stationId ='" + stationId.ToString() +"'";
            BuildDeleteStatement("Station", wClause);
        }

        public void UpdateStation(Station station)
        {
            string statement = BuildUpdateStatement("Station", "id = " + station.Id.ToString(), "availableBatteries = " + station.AvailableBatteries.ToString());
            ExecuteStatement(statement);
        }

        private Station BuildStation(SqlDataReader reader)
        {

            Station station = null;
            station = new Station();

            station.Address = reader["address"].ToString();
            station.ZipCode = Convert.ToInt32(reader["zipCode"]);
            station.City = _dbCity.GetCity(station.ZipCode);
            station.Location = _dbLocation.GetLocation(Convert.ToInt32(reader["locationId"]));
            station.Id = Convert.ToInt32(reader["id"]);
            station.AvailableBatteries = Convert.ToInt32(reader["availableBatteries"]);

            return station;

        }
    }
}
